﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using QuickTest.Core.Compilation;
using QuickTest.Core.Events;
using QuickTest.Core.TestRunner;
using QuickTest.UI.Controls;
using Rhino.Mocks;

namespace QuickTest.Tests.UI.Controls
{
    [TestFixture]
    public class OutputTextBoxTests
    {
        private OutputTextBox outputTextBox;
        private readonly IEventHub eventHub = new EventHub();

        [SetUp]
        public void SetUp()
        {
            outputTextBox = new OutputTextBox();
            outputTextBox.RegisterEvents(eventHub);
        }

        [Test]
        public void Can_Report_Test_Result_NotRun()
        {
            var testResult = new TestResult("MyTestCase", ResultState.NotRun);
            eventHub.RaiseTestFinished(new TestEventArgs(testResult));

            Assert.That(outputTextBox.Text.StartsWith(testResult.ToString()), "Should report test not run result");
        }

        [Test]
        public void Can_Report_Test_Result_Failure()
        {
            var testResult = new TestResult("MyTestCase", ResultState.Failure);
            eventHub.RaiseTestFinished(new TestEventArgs(testResult));

            Assert.That(outputTextBox.Text.StartsWith(testResult.ToString()), "Should report test failure result");
        }

        [Test]
        public void Should_Not_Report_Test_Result_Success()
        {
            var testResult = new TestResult("MyTestCase", ResultState.Success);
            eventHub.RaiseTestFinished(new TestEventArgs(testResult));

            Assert.That(!outputTextBox.Text.StartsWith(testResult.ToString()), "Should not report test success result");
        }

        [Test]
        public void should_clear_text_on_test_run_executed()
        {
            eventHub.RaiseTestFinished(new TestEventArgs(new TestResult("MyTestCase", ResultState.Failure)));
            eventHub.RaiseTestRunExecuted(EventArgs.Empty);

            Assert.That(outputTextBox.Text, Is.Empty, "Should clear test result details");
        }

        [Test]
        public void should_report_compile_started()
        {
            const string message = "Compile started...";

            eventHub.RaiseCompileStarted(new CompileStartedEventArgs(message));

            Assert.That(outputTextBox.Text.StartsWith(message));
        }

        [Test]
        public void should_report_compile_finished()
        {
            const string message = "Compile finished...";

            eventHub.RaiseCompileFinished(new CompileFinishedEventArgs(message, true));

            Assert.That(outputTextBox.Text.StartsWith(message));
        }

        [Test]
        public void should_report_compile_error()
        {
            const string message = "Compile error...";

            eventHub.RaiseCompileError(new CompileErrorEventArgs(message));

            Assert.That(outputTextBox.Text.StartsWith(message));
        }
    }
}
